<?php

//////数据操作模型类封装
namespace app\index\model;
use app\admin\controller\Log;
use think\Model;
use think\Controller;
use think\Request;

class Data extends Model
{
    public function __construct(Request $request = null)
    {
            //think实例化方法日记写入
//            $log= new Log();
//            $log->log_xie('登录系统');
            $this->log=new Log();
            $this->ip=request()->ip();
        parent::__construct($request);
    }
    //自定义初始化
    protected static function init()
    {
            //TODO:自定义的初始化
    }

    /**
     *
     * @param $db 数据库名称
     * @param $table 字段名
     * @param $str 查询值
     * @return bool 检测指定字段数据是否已经存在？ 返回bool类型
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function d_jc($db,$table,$str){
        $new=db($db)->where($table."=".$str)->find();
        if($new){
            return true;
        }else{
            return false;
        }
    }

    /**
     * @param $qy_id 区域id
     * @return int|mixed 取出区域id 成功返回区域名，失败返回null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function d_qqy($qy_id)
    {
        $new=db('quyu')->where('id',$qy_id)->find();
        if($new){
            return $new['name'];
        }else{
            return null;
        }
    }

    /**根据条件取出区域内所有物资信息
     * @param $qy_id 区域id
     * @param $where 查询条件
     * @return int|mixed 成功返回区域数据，失败返回null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function d_qwuzi($qy_id,$where=null)
    {
        if(empty($where)){
            $new=db('wuzi')->where('qy_id',$qy_id)->order('k_time desc')->select();
        }else{
            $new=db('wuzi')->where('qy_id='.$qy_id.' and '.$where)->order('k_time desc')->select();
        }

        if($new){
            return $new;
        }else{
            return null;
        }
    }
    /**取出区域内所有物资的批次数 已通过的物资进行统计
     * @param $qy_id 区域id
     * @return int|mixed 成功返回区域数据，失败返回null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function d_qwzsum($qy_id)
    {
        $new=db('wuzi')->where('qy_id='.$qy_id.' and style=1 and str=2 or '.'qy_id='.$qy_id.' and style=2 and str=2')->order('k_time desc')->select();
        if($new){
            return count($new);
        }else{
            return null;
        }
    }
    /**返回总捐助金额 已通过的物资进行统计
     * @param $qy_id 区域id
     * @return int|mixed 成功返回区域数据，失败返回null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function d_qzjsum($qy_id)
    {
        $new=db('wuzi')->where('qy_id='.$qy_id.' and str=2')->sum('danjia');

        if($new){
            return $new;
        }else{
            return null;
        }
    }
    /**
     * @param $qy_id 区域id
     * @return int|mixed 提交捐助数据 成功返回相关数据，失败返回-1
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function d_wojuan($qy_id)
    {
        $data['qy_id']=$qy_id;
        $data['style']=$_POST['style'];
        $data['name']=$_POST['name'];
        $data['k_time']=time();
        switch($data['style']){
            case 0;
                $data['danjia']=$_POST['danjia'];
                break;
            case 1;
                $data['text']=$_POST['xinxi'];
                break;
            case 2;
                $data['text']=$_POST['xinxi'];
                $data['danjia']=$_POST['danjia'];
                break;
        }
        $new=db('wuzi')->insert($data);
        if($new){
            return $new;
            $this->log->log_xie('IP: '.$this->ip.' 提交了 姓名：'.$data['name'].' 的捐助物资申请 wz_id： '.$new);
        }else{
            return -1;
        }
    }

    /**物资审核通过
     * @return int|string 成功返回更改的数据id,失败返回-1
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function d_wztg($wz_id){
        $data['str']=2;
        $data['e_time']=time();
        $new=db('wuzi')->where('id',$wz_id)->update($data);
        if($new){
            return $new;
            $this->log->log_xie($new['name'].' 的捐助通过了审核');
        }else{
            return -1;
        }
    }
    /**物资撤销
     * @return int|string 成功返回更改的数据id,失败返回-1
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function d_wzcx($wz_id){
        $data['str']=3;
        $data['e_time']=time();
        $new=db('wuzi')->where('id',$wz_id)->update($data);
        if($new){
            return $new;
            $this->log->log_xie($new['name'].' 的捐助被撤销了');
        }else{
            return -1;
        }
    }
}